package com.sfh.cpc.com.ui.look;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;


import com.github.chrisbanes.photoview.PhotoView;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.R;

import java.util.List;

/**
 * 功能描述:
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/5/9
 */
public class ImageAdapter extends PagerAdapter {

    public interface OnImaegCallbackItem {
        void onClick();
    }

    private List<String> data;

    private LayoutInflater layoutInflater;

    private OnImaegCallbackItem callbackItem;


    public ImageAdapter(List<String> data,  OnImaegCallbackItem callbackItem) {
        super();
        this.data = data;
        this.callbackItem = callbackItem;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public View instantiateItem(final ViewGroup container, final int position) {
        if (this.layoutInflater == null) {
            this.layoutInflater = LayoutInflater.from(container.getContext());
        }
        View view = this.layoutInflater.inflate(R.layout.look_image_adapter, container, false);
        container.addView(view);

        PhotoView photoView = view.findViewById(R.id.photoView);
        final ProgressBar progressBar = view.findViewById(R.id.pbLoad);
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callbackItem.onClick();
            }
        });

        progressBar.setVisibility(View.VISIBLE);

        new ImageLoaderEngine(photoView,data.get(position)).memoryPolicy().into(photoView, new ImageLoaderEngine.ImageCallback() {
            @Override
            public void onSuccess(BitmapDrawable bitmap) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onError(Exception e) {
                progressBar.setVisibility(View.GONE);
            }
        });

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}
